/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.cakerun.service.impl;

import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;

import top.continew.admin.cakerun.model.entity.BrandAccountDO;
import top.continew.admin.cakerun.model.entity.ShopInfoDO;
import top.continew.admin.cakerun.service.BrandAccountService;
import top.continew.admin.cakerun.service.ShopInfoService;
import top.continew.starter.extension.crud.model.entity.BaseIdDO;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.admin.common.base.service.BaseServiceImpl;
import top.continew.admin.cakerun.mapper.FeedbackMapper;
import top.continew.admin.cakerun.model.entity.FeedbackDO;
import top.continew.admin.cakerun.model.query.FeedbackQuery;
import top.continew.admin.cakerun.model.req.FeedbackReq;
import top.continew.admin.cakerun.model.resp.FeedbackDetailResp;
import top.continew.admin.cakerun.model.resp.FeedbackResp;
import top.continew.admin.cakerun.service.FeedbackService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 门店评价业务实现
 *
 * @author tangyu
 * @since 2025/07/08 10:47
 */
@Service
@RequiredArgsConstructor
public class FeedbackServiceImpl extends BaseServiceImpl<FeedbackMapper, FeedbackDO, FeedbackResp, FeedbackDetailResp, FeedbackQuery, FeedbackReq> implements FeedbackService {
    private final ShopInfoService shopInfoService;
    private final BrandAccountService brandAccountService;

    @Override
    public PageResp<FeedbackResp> page(FeedbackQuery query, PageQuery pageQuery) {
        PageResp<FeedbackResp> page = super.page(query, pageQuery);
        List<FeedbackResp> list = page.getList();
        List<Integer> accountIds = list.stream().map(FeedbackResp::getAccountId).distinct().toList();
        List<Integer> shopIds = list.stream().map(FeedbackResp::getShopId).distinct().toList();
        List<BrandAccountDO> brandAccountDOS = this.brandAccountService.findByIds(accountIds);
        List<ShopInfoDO> shopInfoDOS = this.shopInfoService.findByShopIds(shopIds);
        Map<Long, String> accountMap = brandAccountDOS.stream()
            .collect(Collectors.toMap(BaseIdDO::getId, BrandAccountDO::getBrand, (a, b) -> b));
        Map<Integer, String> shopInfoMap = shopInfoDOS.stream()
            .collect(Collectors.toMap(ShopInfoDO::getShopId, ShopInfoDO::getPoiName, (a, b) -> b));
        list.forEach(feedbackResp -> {
            String accountName = accountMap.get(Long.valueOf(feedbackResp.getAccountId()));
            feedbackResp.setAccountName(accountName);
            String shopName = shopInfoMap.get(feedbackResp.getShopId());
            feedbackResp.setShopName(shopName);
        });
        return page;
    }
}